package com.tanhua.admin.service;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tanhua.admin.exception.BusinessException;
import com.tanhua.admin.mapper.AnalysisMapper;
import com.tanhua.admin.mapper.LogMapper;
import com.tanhua.model.domain.Analysis;
import com.tanhua.model.vo.AnalysisUsersVo;
import com.tanhua.model.vo.DataPointVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class AnalysisService {

    @Autowired
    private LogMapper logMapper;

    @Autowired
    private AnalysisMapper analysisMapper;

    /**
     * 定时统计tb_log表中的数据，保存或者更新tb_analysis表
     * 1、查询tb_log表（注册用户数，登录用户数，活跃用户数，次日留存）
     * 2、构造Analysis对象
     * 3、保存或者更新
     */
    public void analysis() throws ParseException {
        //1、定义查询的日期
        String todayStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String yesdayStr = DateUtil.yesterday().toString("yyyy-MM-dd");
        //2、统计数据-注册数量
        Integer regCount = logMapper.queryByTypeAndLogTime("0102", todayStr);
        //3、统计数据-登录数量
        Integer loginCount = logMapper.queryByTypeAndLogTime("0101", todayStr);
        //4、统计数据-活跃数量
        Integer activeCount = logMapper.queryByLogTime(todayStr);
        //5、统计数据-次日留存
        Integer numRetention1d = logMapper.queryNumRetention1d(todayStr, yesdayStr);
        //6、构造Analysis对象
        //7、根据日期查询数据
        QueryWrapper<Analysis> qw = new QueryWrapper<Analysis>();
        qw.eq("record_date", new SimpleDateFormat("yyyy-MM-dd").parse(todayStr));
        Analysis analysis = analysisMapper.selectOne(qw);
        //8、如果存在，更新，如果不存在保存
        if (analysis != null) {
            analysis.setNumRegistered(regCount);
            analysis.setNumLogin(loginCount);
            analysis.setNumActive(activeCount);
            analysis.setNumRetention1d(numRetention1d);
            analysis.setUpdated(new Date());
            analysisMapper.updateById(analysis);

        } else {
            analysis = new Analysis();
            analysis.setNumRegistered(regCount);
            analysis.setNumLogin(loginCount);
            analysis.setNumActive(activeCount);
            analysis.setNumRetention1d(numRetention1d);
            analysis.setRecordDate(new SimpleDateFormat("yyyy-MM-dd").parse(todayStr));
            analysis.setCreated(new Date());
            analysis.setUpdated(new Date());

            analysisMapper.insert(analysis);
        }
    }
    public Integer queryCumulativeUsers() {
        return analysisMapper.queryCumulativeUsers();
    }

    public Analysis querySummary(Date now) {
        //5、根据当前时间查询AnalysisByDay数据
        LambdaQueryWrapper<Analysis> qw = Wrappers.<Analysis>lambdaQuery();
        qw.eq(Analysis::getRecordDate, DateUtil.format(now, "yyyy-MM-dd"));
        return analysisMapper.selectOne(qw);
    }

    /**
     * 新增用户,活跃用户,次日留存率
     * @param sd
     * @param ed
     * @param type
     * @return
     */
    public AnalysisUsersVo getUserCount(Long sd, Long ed, Integer type) {
        String column;
        switch (type) {
            case 101:
                column = "num_registered";
                break;
            case 102:
                column = "num_active";
                break;
            case 103:
                column = "num_retention1d";
                break;
            default:
                throw new BusinessException("参数格式不正确");
        }
        String thisYearStartDate = DateUtil.date(sd).toDateStr();
        String thisYearEndDate = DateUtil.date(ed).toDateStr();
        List<DataPointVo> thisYearDateCount =  analysisMapper.thisYearStartDate(thisYearStartDate , thisYearEndDate , column);

        String lastYearStartDate = DateUtil.date(sd).offset(DateField.YEAR, -1).toDateStr();
        String lastYearEndDate = DateUtil.date(ed).offset(DateField.YEAR, -1).toDateStr();
        List<DataPointVo> lastYearDateCount =  analysisMapper.thisYearStartDate(lastYearStartDate , lastYearEndDate ,column);

        AnalysisUsersVo vo = new AnalysisUsersVo();
        vo.setThisYear(thisYearDateCount);
        vo.setLastYear(lastYearDateCount);
        return vo;
    }
}

